package com.ruoyi.web.controller.aggregation;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.domain.aggregation.Account;
import com.ruoyi.system.service.aggregation.IAccountService;

/**
 * 账户Controller
 * 
 * @author ruoyi
 * @date 2025-01-01
 */
@RestController
@RequestMapping("/agg/account")
public class AccountController
{
    @Autowired
    private IAccountService accountService;

    /**
     * 查询账户详情
     */
    @GetMapping("/getInfo")
    public AjaxResult getInfo(@RequestParam(required = false) Long userId)
    {
        try
        {
            Account account;
            if (userId != null) {
                // 传了userId，查询指定用户账户
                account = accountService.selectAccountByUserId(userId);
            } else {
                // 没传userId，查询当前登录用户账户
                Long currentUserId = SecurityUtils.getUserId();
                if (currentUserId == null) {
                    return AjaxResult.error("用户未登录");
                }
                account = accountService.selectAccountByUserId(currentUserId);
            }
            return AjaxResult.success(account);
        }
        catch (Exception e)
        {
            return AjaxResult.error(e.getMessage());
        }
    }

    /**
     * 获取当前登录用户账户信息
     */
    @GetMapping("/current")
    public AjaxResult getCurrentAccount()
    {
        try
        {
            Long currentUserId = SecurityUtils.getUserId();
            if (currentUserId == null) {
                return AjaxResult.error("用户未登录");
            }
            Account account = accountService.selectAccountByUserId(currentUserId);
            return AjaxResult.success(account);
        }
        catch (Exception e)
        {
            return AjaxResult.error(e.getMessage());
        }
    }

    /**
     * 账户详情接口（别名，兼容前端调用）
     */
    @GetMapping("/detail")
    public AjaxResult detail(@RequestParam(required = false) Long userId)
    {
        try
        {
            Account account;
            if (userId != null) {
                // 传了userId，查询指定用户账户
                account = accountService.selectAccountByUserId(userId);
            } else {
                // 没传userId，查询当前登录用户账户
                Long currentUserId = SecurityUtils.getUserId();
                if (currentUserId == null) {
                    return AjaxResult.error("用户未登录");
                }
                account = accountService.selectAccountByUserId(currentUserId);
            }
            return AjaxResult.success(account);
        }
        catch (Exception e)
        {
            return AjaxResult.error(e.getMessage());
        }
    }
}

